import 'package:provider/provider.dart';
import 'package:zhongyao/global.dart';
import 'package:zhongyao/http/api.dart';
import 'package:flutter/material.dart';
import 'package:zhongyao/main_manager.dart';
import 'package:zhongyao/toast.dart';
import 'package:zhongyao/utils.dart';

class DetailPage extends StatefulWidget {
  DetailPage({Key key, this.id}) : super(key: key);
  final String id;
  @override
  _DetailPageState createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  var _data;
  var _info;
  @override
  void initState() {
    String _id = widget.id;
    detail(_id).then((res) {
      setState(() {
        _data = res;
        _info = {
          'id': _id,
          'title': _data['title'],
          'from': _data['content'][0],
        };
      });
    });
    super.initState();
  }

  List<Widget> renderDesc(items) {
    List<Widget> list = [];
    for (var i = 0; i < items.length; i++) {
      var item = items[i];
      if (item != '' && i != 0 && item != '此中草药来源于网络，使用前请遵医嘱。') {
        list.add(SizedBox(height: 5));
        list.add(
          Container(
            padding: EdgeInsets.all(8),
            width: MediaQuery.of(context).size.width,
            decoration: BoxDecoration(
              border: Border(
                top: BorderSide(
                  width: 1,
                  color: Global.primaryColor.withOpacity(0.1),
                ),
              ),
            ),
            child: Text(
              ' ' + item,
              style: TextStyle(
                color: Global.fontSecondColor.withOpacity(0.9),
                fontSize: 15,
                fontWeight: FontWeight.w400,
                height: 2,
              ),
            ),
          ),
        );
      }
    }
    return list;
  }

  @override
  Widget build(BuildContext context) {
    MainManager _mainManager = Provider.of<MainManager>(context);
    return _data == null
        ? Scaffold(
            body: Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              color: Global.primaryColor,
              child: Center(child: CircularProgressIndicator()),
            ),
          )
        : Scaffold(
            appBar: AppBar(
              title: Text(_data['title'], style: TextStyle(fontSize: 14)),
              elevation: 0,
              backgroundColor: Global.primaryColor,
              actions: <Widget>[
                IconButton(
                  icon: Icon(
                    _mainManager.isLike(_info)
                        ? Icons.favorite
                        : Icons.favorite_border,
                    color: _mainManager.isLike(_info)
                        ? Color(0xFFC62A1F)
                        : Global.backgroundColor,
                  ),
                  tooltip: '收藏',
                  onPressed: () {
                    if (_mainManager.isLike(_info)) {
                      Utils.showConfirmDialog(
                        context: context,
                        content: '确认取消收藏？',
                        confirmCallback: () {
                          _mainManager.like(_info);
                          Toast.show(context, msg: '取消收藏成功', duration: 2);
                        },
                        showCancelButton: true,
                      );
                    } else {
                      _mainManager.like(_info);
                      Toast.show(context, msg: '收藏成功', duration: 2);
                    }
                  },
                )
              ],
            ),
            body: Container(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              decoration: BoxDecoration(
                color: Global.backgroundColor,
              ),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      '　来源：${_data['content'][0]}',
                      style: TextStyle(
                        color: Global.fontColor,
                        fontSize: 20,
                        fontWeight: FontWeight.w500,
                        height: 1.9,
                      ),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: renderDesc(_data['content']),
                    ),
                    SizedBox(height: 35),
                  ],
                ),
              ),
            ),
          );
  }
}
